package com.wwwarehouse.contract.program_operation.resource_ship;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.contract.bean.CustomerAndResListBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ResourceShipCheckEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/22.
 */

public class SelectCustomerFragment extends BaseFragment implements View.OnClickListener {
	private TextView mTv1 , mTv2 , mTv3 , mTv4 , mTv5 , mTv6;
	private List<CustomerAndResListBean.Customers> list = new ArrayList<>();
	private Button mConfirmBtn;
	private int position;
	private boolean isFunction;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_select_customer, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mTv1 = (TextView) view.findViewById(R.id.tv1);
		mTv2 = (TextView) view.findViewById(R.id.tv2);
		mTv3 = (TextView) view.findViewById(R.id.tv3);
		mTv4 = (TextView) view.findViewById(R.id.tv4);
		mTv5 = (TextView) view.findViewById(R.id.tv5);
		mTv6 = (TextView) view.findViewById(R.id.tv6);
		mConfirmBtn = (Button) view.findViewById(R.id.confirm_btn);

		mTv1.setVisibility(View.GONE);
		mTv2.setVisibility(View.GONE);
		mTv3.setVisibility(View.GONE);
		mTv4.setVisibility(View.GONE);
		mTv5.setVisibility(View.GONE);
		mTv6.setVisibility(View.GONE);

		mTv1.setOnClickListener(this);
		mTv2.setOnClickListener(this);
		mTv3.setOnClickListener(this);
		mTv4.setOnClickListener(this);
		mTv5.setOnClickListener(this);
		mTv6.setOnClickListener(this);
		mConfirmBtn.setOnClickListener(this);
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof SelectCustomerFragment) {
			mActivity.setTitle(R.string.select_customers_title);
		}
	}

	@Override
	public void requestDatas() {

		if (getArguments() != null && getArguments().getParcelableArrayList(ContractConstant.KEY_BUNDLE_PARA_DATA) != null) {
			list.clear();
			position = getArguments().getInt(ContractConstant.KEY_BUNDLE_CUSTOMER_POSITION);
			list = (List<CustomerAndResListBean.Customers>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_PARA_DATA);
			int size = list.size();
			if (size == 1) {
				mTv1.setVisibility(View.VISIBLE);
				mTv1.setText(list.get(0).getName());
				changeSelectBoolean(0 , true);
			} else if (size == 2) {
				mTv1.setVisibility(View.VISIBLE);
				mTv2.setVisibility(View.VISIBLE);

				mTv1.setText(list.get(0).getName());
				mTv2.setText(list.get(1).getName());
				changeSelectBoolean(0, true);
				changeSelectBoolean(1, true);

			} else if (size == 3) {
				mTv1.setVisibility(View.VISIBLE);
				mTv2.setVisibility(View.VISIBLE);
				mTv3.setVisibility(View.VISIBLE);

				mTv1.setText(list.get(0).getName());
				mTv2.setText(list.get(1).getName());
				mTv3.setText(list.get(2).getName());
				changeSelectBoolean(0, true);
				changeSelectBoolean(1, true);
				changeSelectBoolean(2, true);

			} else if (size == 4) {
				mTv1.setVisibility(View.VISIBLE);
				mTv2.setVisibility(View.VISIBLE);
				mTv3.setVisibility(View.VISIBLE);
				mTv4.setVisibility(View.VISIBLE);

				mTv1.setText(list.get(0).getName());
				mTv2.setText(list.get(1).getName());
				mTv3.setText(list.get(2).getName());
				mTv4.setText(list.get(3).getName());
				changeSelectBoolean(0, true);
				changeSelectBoolean(1, true);
				changeSelectBoolean(2, true);
				changeSelectBoolean(3, true);

			} else if (size == 5) {
				mTv1.setVisibility(View.VISIBLE);
				mTv2.setVisibility(View.VISIBLE);
				mTv3.setVisibility(View.VISIBLE);
				mTv4.setVisibility(View.VISIBLE);
				mTv5.setVisibility(View.VISIBLE);

				mTv1.setText(list.get(0).getName());
				mTv2.setText(list.get(1).getName());
				mTv3.setText(list.get(2).getName());
				mTv4.setText(list.get(3).getName());
				mTv5.setText(list.get(4).getName());
				changeSelectBoolean(0, true);
				changeSelectBoolean(1, true);
				changeSelectBoolean(2, true);
				changeSelectBoolean(3, true);
				changeSelectBoolean(4, true);

			} else if (size == 6) {
				mTv1.setVisibility(View.VISIBLE);
				mTv2.setVisibility(View.VISIBLE);
				mTv3.setVisibility(View.VISIBLE);
				mTv4.setVisibility(View.VISIBLE);
				mTv5.setVisibility(View.VISIBLE);
				mTv6.setVisibility(View.VISIBLE);

				mTv1.setText(list.get(0).getName());
				mTv2.setText(list.get(1).getName());
				mTv3.setText(list.get(2).getName());
				mTv4.setText(list.get(3).getName());
				mTv5.setText(list.get(4).getName());
				mTv6.setText(list.get(5).getName());
				changeSelectBoolean(0, true);
				changeSelectBoolean(1, true);
				changeSelectBoolean(2, true);
				changeSelectBoolean(3, true);
				changeSelectBoolean(4, true);
				changeSelectBoolean(5, true);

			}
		}


	}

	@Override
	public void onClick(View v) {
		int clickId = v.getId();
		if (clickId == R.id.tv1) {
			changeSelectBoolean(0, false);

		} else if (clickId == R.id.tv2) {
			changeSelectBoolean(1, false);

		} else if (clickId == R.id.tv3) {
			changeSelectBoolean(2, false);

		} else if (clickId == R.id.tv4) {
			changeSelectBoolean(3, false);

		} else if (clickId == R.id.tv5) {
			changeSelectBoolean(4, false);

		} else if (clickId == R.id.tv6) {
			changeSelectBoolean(5, false);

		} else if (clickId == R.id.confirm_btn) {
			EventBus.getDefault().post(new ResourceShipCheckEvent(position , list ));
			popFragment();
		}

	}

	private void changeSelectBoolean(int i , boolean check) {
		if (!check) {
			if (list.get(i).isSelected()) {
				list.get(i).setSelected(false);
			} else {
				list.get(i).setSelected(true);
			}
		}

		if (i == 0) {
			changeTvBg(0 , mTv1);
		} else if (i == 1) {
			changeTvBg(1, mTv2);
		} else if (i == 2) {
			changeTvBg(2, mTv3);
		} else if (i == 3) {
			changeTvBg(3, mTv4);
		} else if (i == 4) {
			changeTvBg(4, mTv5);
		} else if (i == 5) {
			changeTvBg(5, mTv6);
		}
		checkButton();
	}

	private void changeTvBg(int i ,TextView tv) {
		if (list.get(i).isSelected()) {
			tv.setBackground(mActivity.getResources().getDrawable(R.drawable.stroke_blue_sold_whit_background));
			tv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c1_337cff));
		} else {
			tv.setBackground(mActivity.getResources().getDrawable(R.drawable.text_view_gray_background));
			tv.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));
		}
	}


	/**
	 * 校验按钮是否可点击
	 */
	private boolean checkClickable() {
		boolean canClick = false;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isSelected()) {
				canClick = true;
			}
		}
		if (canClick) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 校验按钮是否可点击
	 */
	private void checkButton() {
		if (checkClickable()) {
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(getResources().getDrawable(R.drawable.common_btn1_normal_shape));
		} else {
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
		}
	}
}
